using System;
using System.Collections.Generic;
using OracleInternal.Common;
using OracleInternal.Core;
using OracleInternal.ServiceObjects;

namespace OracleInternal.TTC.Accessors
{
	internal class TTCNumberAccessor : Accessor
	{
		internal const int NUM_MAX_LENGTH = 21;

		internal TTCNumberAccessor(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine, bool bForBind)
			: base(colMetaData, marshallingEngine, bForBind)
		{
			if (!bForBind)
			{
				InitForDataAccess(colMetaData.m_maxLength);
			}
		}

		internal override void InitForDataAccess(int max_len)
		{
			m_internalTypeMaxLength = 21;
			if (max_len > 0 && max_len < m_internalTypeMaxLength)
			{
				m_internalTypeMaxLength = max_len;
			}
			m_byteLength = m_internalTypeMaxLength + 1;
		}

		internal override byte[] GetByteRepresentation(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex)
		{
			byte[] result = null;
			int length = 0;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length);
				if (length > 0)
				{
					result = new byte[length];
					Accessor.CopyDataToUserBuffer(m_colDataSegments, 0, result, 0, length);
					return result;
				}
				return result;
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
		}

		internal override byte[] GetByteRepresentation(int currentRow)
		{
			byte[] array = null;
			int num = m_totalLengthOfData[currentRow];
			if (num > 0)
			{
				List<ArraySegment<byte>> list = m_RowDataSegments[currentRow];
				if (list != null)
				{
					array = new byte[num];
					Accessor.CopyDataToUserBuffer(list, 0, array, 0, num);
				}
			}
			return array;
		}

		internal override int GetInt(int currentRow)
		{
			int result = 0;
			int num = m_totalLengthOfData[currentRow];
			if (num > 0)
			{
				List<ArraySegment<byte>> list = m_RowDataSegments[currentRow];
				byte[] array = null;
				int num2 = 0;
				if (list != null)
				{
					if (list.Count > 1)
					{
						array = new byte[num];
						num2 = 0;
						Accessor.CopyDataToUserBuffer(list, 0, array, 0, num);
					}
					else
					{
						array = list[0].Array;
						num2 = list[0].Offset;
					}
					result = HelperClass.GetInt(array, num2, num);
				}
			}
			return result;
		}

		internal override double GetDouble(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex)
		{
			byte[] byteRep = null;
			return GetDouble(dataUnmarshaller, currentRow, columnIndex, out byteRep);
		}

		internal override double GetDouble(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex, out byte[] byteRep)
		{
			double result = 0.0;
			int length = 0;
			byteRep = null;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length);
				if (length > 0)
				{
					byteRep = new byte[length];
					Accessor.CopyDataToUserBuffer(m_colDataSegments, 0, byteRep, 0, length);
					return OracleNumberCore.lnxnur(byteRep);
				}
				return result;
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
		}

		internal override double GetDouble(int currentRow)
		{
			double result = 0.0;
			int num = m_totalLengthOfData[currentRow];
			if (num > 0)
			{
				List<ArraySegment<byte>> list = m_RowDataSegments[currentRow];
				byte[] array = null;
				if (list != null)
				{
					array = new byte[num];
					Accessor.CopyDataToUserBuffer(list, 0, array, 0, num);
					result = OracleNumberCore.lnxnur(array);
				}
			}
			return result;
		}

		internal override float GetFloat(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex)
		{
			byte[] byteRep = null;
			return GetFloat(dataUnmarshaller, currentRow, columnIndex, out byteRep);
		}

		internal override float GetFloat(DataUnmarshaller dataUnmarshaller, int currentRow, int columnIndex, out byte[] byteRep)
		{
			float result = 0f;
			int length = 0;
			byteRep = null;
			try
			{
				dataUnmarshaller.StartAccumulatingColumnData(currentRow, columnIndex, m_colDataSegments);
				dataUnmarshaller.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, ref length);
				if (length > 0)
				{
					byteRep = new byte[length];
					Accessor.CopyDataToUserBuffer(m_colDataSegments, 0, byteRep, 0, length);
					return HelperClass.GetFloat(byteRep, 0, length);
				}
				return result;
			}
			finally
			{
				m_colDataSegments.Clear();
				dataUnmarshaller.m_bAccumulateByteSegments = false;
				dataUnmarshaller.m_dataSegments = null;
			}
		}

		internal override float GetFloat(int currentRow)
		{
			float result = 0f;
			int num = m_totalLengthOfData[currentRow];
			if (num > 0)
			{
				List<ArraySegment<byte>> list = m_RowDataSegments[currentRow];
				byte[] array = null;
				int offset = 0;
				if (list != null)
				{
					if (list.Count == 1)
					{
						array = list[0].Array;
						offset = list[0].Offset;
					}
					else
					{
						array = new byte[num];
						Accessor.CopyDataToUserBuffer(list, 0, array, 0, num);
					}
					result = HelperClass.GetFloat(array, offset, num);
				}
			}
			return result;
		}
	}
}
